// 清除缓存页
//  MZY_DeleteViewController.m
//  FullHouse
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_DeleteViewController.h"

@interface MZY_DeleteViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation MZY_DeleteViewController

- (void)dealloc
{
    [self.tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
}

#pragma mark 创建界面
- (void)createView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 创建"tableView"
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.tableView release];
    
}

#pragma mark "tableView"的第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

#pragma mark "tableView"的第二个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.textLabel.text = @"清除缓存";
    return cell;
}

//首先获取缓存文件的路径
-(NSString *)getPath{
    //沙盒目录下library文件夹下的cache文件夹就是缓存文件夹
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    return path;
}

-(CGFloat)folderSizeWithPath:(NSString *)path{
    //初始化文件管理类
    NSFileManager * fileManager = [NSFileManager defaultManager];
    float folderSize = 0.0;
    if ([fileManager fileExistsAtPath:path]){
        //如果存在
        //计算文件的大小
        NSArray * fileArray = [fileManager subpathsAtPath:path];
        for (NSString * fileName in fileArray){
            //获取每个文件的路径
            NSString * filePath = [path stringByAppendingPathComponent:fileName];
            //计算每个子文件的大小
            long fileSize = [fileManager attributesOfItemAtPath:filePath error:nil].fileSize;//字节数
            folderSize = folderSize + fileSize / 1024.0 / 1024.0;
        }
        //删除缓存文件
        [self deleteFileSize:folderSize];
        return folderSize;
    }
    return 0;
}

#pragma mark - 弹出是否删除的一个提示框，并且告诉用户目前有多少缓存
-(void)deleteFileSize:(CGFloat)folderSize{
    if (folderSize > 0.01){
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存大小:%.2fM,是否清除？",folderSize] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            [self alertAction];
            // 清理成功
            UIAlertController *success = [UIAlertController alertControllerWithTitle:@"提示" message:@"清理成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
            [success addAction:okAction];
            [self presentViewController:success animated:YES completion:nil];
        }];
        [alertCon addAction:cancelAction];
        [alertCon addAction:okAction];
        [self presentViewController:alertCon animated:YES completion:nil];
    }else{
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"缓存已经全部清理" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertCon addAction:okAction];
        [self presentViewController:alertCon animated:YES completion:nil];
    }
}

-(void)alertAction{
    //彻底删除文件
    [self clearCacheWith:[self getPath]];
}

-(void)clearCacheWith:(NSString *)path{
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]){
        NSArray * fileArray = [fileManager subpathsAtPath:path];
        for (NSString * fileName in fileArray)
        {
            //可以过滤掉特殊格式的文件
            //            if ([fileName hasSuffix:@".png"])
            //            {
            //                NSLog(@"不删除");
            //            }
            //            else
            //            {
            //获取每个子文件的路径
            NSString * filePath = [path stringByAppendingPathComponent:fileName];
            //移除指定路径下的文件
            [fileManager removeItemAtPath:filePath error:nil];
            //            }
        }
    }
}

#pragma mark "tableView"的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 清除缓存
    [self folderSizeWithPath:[self getPath]];
}

#pragma mark 返回按钮
- (void)backAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
